package com.iccaplayer;

import com.example.iccaplayer.R;
import com.example.iccaplayer.R.layout;
import com.example.iccaplayer.R.menu;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class SettingKeyActivity extends Activity {
	private static final String TAG = "SettingKeyActivity";
	private static final String[] PITCHES = new String[]{"c", "cs", "d", "ds", "e", "f", "fs", "g", "gs", "a", "as", "b"};
	
	Intent mainIntent;
	Bundle extras;
	
	Spinner octaveSpin, pitchesSpin;
	ArrayAdapter<CharSequence> octaveArray, pitchesArray;
	Button okBtn, cancelBtn;
	
	int insPos, row, col, octavePos, pitchesPos;
	String key, octave, pitches;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_key);
		
		octaveSpin = (Spinner)findViewById(R.id.Octave);
		octaveArray = ArrayAdapter.createFromResource(this, R.array.OctaveArray, android.R.layout.simple_spinner_item);
		pitchesSpin = (Spinner)findViewById(R.id.Pitches);
		pitchesArray = ArrayAdapter.createFromResource(this, R.array.PitchesArray, android.R.layout.simple_spinner_item);
		okBtn = (Button)findViewById(R.id.SettingOkBtn);
		cancelBtn = (Button)findViewById(R.id.SettingCancelBtn);
		
		mainIntent = getIntent();
		extras = mainIntent.getExtras();
		
		// Get Information
		if(extras != null){
			insPos = extras.getInt("Pos");
			row = extras.getInt("Row");
			col = extras.getInt("Col");		
			key = extras.getString("Key");
			
			Log.d("keyFlow", "onCreate():: insPos:" + insPos + " row: " + row + " col: " + col + " key: " + key);
			if(key != null){
				if(key.length() > 2){
					pitches = key.substring(0, 2);
					octave = key.substring(2);			
				}
				else{					
					pitches = key.substring(0,1);
					octave = key.substring(1);
				}				
				
				octavePos = Integer.parseInt(octave) - 1;
				for(int i=0; i<12; i++){
					if(pitches.equalsIgnoreCase(PITCHES[i])){
						pitchesPos = i;
						break;
					}
				}
			}
			Log.d("keyFlow", "onCreate():: key:" + key + " octave: " + octave + " pitches: " + pitches);
		}	
		
		// Setting Octave Spinner
		octaveArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		octaveSpin.setAdapter(octaveArray);
		octaveSpin.setSelection(octavePos);
		octaveSpin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View spin,
					int position, long arg3) {	
				octavePos = octaveSpin.getSelectedItemPosition();
				octave = octaveArray.getItem(octavePos) + "";				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {		
			}
		});
		// Setting Ptiches Spinner
		pitchesArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		pitchesSpin.setAdapter(pitchesArray);
		pitchesSpin.setSelection(pitchesPos);
		pitchesSpin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View spin,
					int position, long arg3) {
				pitchesPos = pitchesSpin.getSelectedItemPosition();
				pitches = pitchesArray.getItem(pitchesPos) + "";
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		// Close Information of Instrument Activity
		okBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {			
				Log.d("keyFlow", "onActivityResult()ok:: insPos:" + insPos + " row: " + row + " col: " + col  + " octave: " + octave + " pitches: " + pitches);
				
				mainIntent.putExtra("Result", true);
				mainIntent.putExtra("InsPos", insPos);
				mainIntent.putExtra("SeqRow", row);
				mainIntent.putExtra("SeqCol", col);
				mainIntent.putExtra("InsOctave", octave);
				mainIntent.putExtra("InsPitches", pitches);
				setResult(RESULT_OK, mainIntent);
				finish();
			}
		});		
		
		// Close Information of Instrument Activity
		cancelBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mainIntent.putExtra("Result", false);
				mainIntent.putExtra("InsPos", insPos);
				mainIntent.putExtra("SeqRow", row);
				mainIntent.putExtra("SeqCol", col);
				setResult(RESULT_OK, mainIntent);
				finish();
			}
		});		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.setting_key, menu);
		return true;
	}

}
